=dict(cheery=[12,100],cookies=[18,200],greentea=[15,140],mango=[10,200]) icecream
금융공학프로그래밍3, Quiz2
1. Dictionary
(1)
Change the price of ‘cookies’ as 13
'cookies'][0]=13
icecream['cookies'] icecream[
[13, 200]
(2)
Add the following information to the dictionary ‘icecream’. - ‘Strawberry’ is priced at 12 and inventory is 170. - ‘pistachio’ is priced at 15 and inventory is 100.
=dict(Strawberry=[12,170],pistachio=[15,100])
update_dict
icecream.update(update_dict) icecream
{'cheery': [12, 100],
'cookies': [13, 200],
'greentea': [15, 140],
'mango': [10, 200],
'Strawberry': [12, 170],
'pistachio': [15, 100]}
2. Dictionary2
=dict(key1=1,key2=3,key3=2,key10=7)
d1=dict(key3=1,key2=2,key7=2,key5=4,key1=7,key9=8,key0=7) d2
(1)
Select the largest two values of the dictionary ‘d2’.
=list(d2.values())
d2_list
d2_list.sort()
d2_list.reverse()0:2] d2_list[
[8, 7]
(2)
Create a set of values which are the unique values of ‘d2’.
set(d2.values())
{1, 2, 4, 7, 8}
(3)
Write code that returns True if keys of ‘d1’ are all contained in the key of ‘d2’ and False otherwise.
set(d1.keys()) <= set(d2.keys())
False
(4)
Create a set of values which are common in both ‘d1’ and ‘d2’
set(d1.values())&set(d2.values())
{1, 2, 7}
3. if statement
For any arbitrary string object A, if the first character of A is ‘#’, we want it to be replaced with ‘*’. Otherwise, we want to add ‘*’ as the first character of A. Write appropriate code using an if statement for this
="#abcde"
A
if A[0]=="#":
="*"+A[1:]
Aelse:
="*"+A
A
A
'*abcde'
4. for loop
Calculate the following by using ‘for’ loop : \(\sum_{i=1}^{10} i^4\)
=int()
result
for i in range(1,11):
+=i**4
result
result
25333